package com.example.backend.service.impl;


import com.example.backend.entity.Driver;
import com.example.backend.repository.DriverRepository;
import com.example.backend.service.DriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DriverServiceImpl implements DriverService {
    @Autowired
    private DriverRepository driverRepository;

    @Override
    public Driver save(Driver driver) {
        return driverRepository.save(driver);
    }

    @Override
    public List<Driver> findAll() {
        return driverRepository.findAll();
    }

    @Override
    public List<Driver> findByName(String name) {
        return driverRepository.findByName(name);
    }

    @Override
    public List<Driver> findByIdentity(String identity) {
        return driverRepository.findByIdentity(identity);
    }

    @Override
    public List<Driver> findByAge(Integer age) {
        return driverRepository.findByAge(age);
    }

    @Override
    public List<Driver> findByAddress(String address) {
        return driverRepository.findByAddress(address);
    }

    @Override
    public List<Driver> findByPhone(String phone) {
        return driverRepository.findByPhone(phone);
    }

    @Override
    public List<Driver> findByLicense(String license) {
        return driverRepository.findByLicense(license);
    }

    @Override
    public List<Driver> findByState(String state) {
        return driverRepository.findByState(state);
    }

    @Override
    public List<Driver> findById(Integer id) {
        if (!driverRepository.findById(id).isPresent()){
            return null;
        }
        else {
            Driver d =driverRepository.findById(id).get();
            return List.of(d);
        }
    }

    @Override
    public boolean findName(String name) {
        if (driverRepository.findByName(name).isEmpty()){
            return false;
        }
        return true;
    }

    @Override
    public boolean findNameAndId(Integer id, String name) {
        if (driverRepository.findByName(name).isEmpty()){
            return false;
        }
        else if (driverRepository.findByName(name).get(0).getId()==id){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public void delete(Integer id) {
        Driver driver = driverRepository.findById(id).get();
        driverRepository.delete(driver);
    }

    @Override
    public Driver update(Driver driver) {
        return driverRepository.save(driver);
    }
}
